package com.swt.dao.Tdao.impl;

import com.swt.dao.Tdao.MemberDao;
import com.swt.domain.Member;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository(value = "TMemberDao")
public class MemberDaoImpl implements MemberDao {
    @Resource
    private HibernateTemplate hibernateTemplate;


    @Override
    public boolean memberAdd(Member member, int clubClubId) {
        SessionFactory sessionFactory = hibernateTemplate.getSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        member.setClubClubId(clubClubId);
        try {
            session.save(member);

        }catch (Exception e){
            return false;
        }

        return true;
    }
    //会员卡查询方法
    @Override
    public List<Member> memberQuery(int clubClubId) {
        SessionFactory sessionFactory = hibernateTemplate.getSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        List<Member> memberList  = session.createQuery("from Member where clubClubId = ?").setParameter(0,clubClubId).list();
        return memberList;
    }

//    会员卡删除方法
    @Override
    public boolean memberDelete(int id) {
        SessionFactory sessionFactory = hibernateTemplate.getSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        Member member = session.get(Member.class,id);

        try {
            session.delete(member);
        }catch (Exception e){
            return false;
        }
        return true;
    }
//根据id查询
    @Override
    public List<Member> memberQuery2(int id) {
        SessionFactory sessionFactory = hibernateTemplate.getSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        String sql="from Member where id = ?";
        Query query = session.createQuery(sql);
        List<Member> memberList = query.setParameter(0,id).list();

        return memberList;

    }
    //    更新会员卡
    @Override
    public boolean memberUpdate(Member member) {

        try {
            hibernateTemplate.update(member);
        }catch (Exception e){
            return false;
        }

        return true;
    }



    public HibernateTemplate getHibernateTemplate() {
        return hibernateTemplate;
    }

    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }

}
